package com.wyzz.hr.action;

import java.util.ArrayList;
import java.util.List;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.bo.RoleBo;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.pojo.Role;
import com.wyzz.hr.spring.SpringBeanFactory;

public class RoleListAction extends BaseAction {

	public RoleListAction() {
	}

	public String execute() throws Exception {
		roleList = ((RoleBo) SpringBeanFactory.getBean("roleService"))
				.getRoleList();
		return "success";
	}

	public String view() throws Exception {
		if (role == null)
			return "error";
		role = getRoleBo().getRole(role.getId());
		List list = new ArrayList();
		String auths[] = role.getRoleAuthority().split(",");
		for (int i = 0; i < auths.length; i++)
			list.add(Integer.valueOf(Integer.parseInt(auths[i])));

		authorities = getRoleBo().getAllAuthoritys(list);
		if (role == null) {
			addActionError("\u65E0\u6B64\u89D2\u8272!");
			return "error";
		} else {
			return "success";
		}
	}

	public String del() throws Exception {
		if (roleNo == null || roleNo.toString().trim().equals("")) {
			clearErrorsAndMessages();
			return "success";
		}
		String callBack = getRoleBo().delRole(roleNo, getUserBo());
		if ("yes".equals(callBack)) {
			addSuccessInfo("\u5220\u9664\u6210\u529F\u3002");
			roleList = roleBo.getRoleList();
		} else if ("err".equals(callBack))
			addErrorInfo("\u6B64\u89D2\u8272\u6709\u7528\u6237\u4E0E\u4E4B\u5173\u8054\uFF0C\u4E0D\u80FD\u5220\u9664\uFF01");
		else if ("ref".equals(callBack))
			addErrorInfo("\u4E0D\u80FD\u91CD\u590D\u63D0\u4EA4\uFF01");
		return "success";
	}

	public RoleBo getRoleBo() {
		if (roleBo == null)
			roleBo = (RoleBo) SpringBeanFactory.getBean("roleService");
		return roleBo;
	}

	public UserBo getUserBo() {
		if (userBo == null)
			userBo = (UserBo) getBean("userService");
		return userBo;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List getRoleList() {
		return roleList;
	}

	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}

	public Integer getRoleNo() {
		return roleNo;
	}

	public void setRoleNo(Integer roleNo) {
		this.roleNo = roleNo;
	}

	public List getAuthorities() {
		return authorities;
	}

	public void setAuthorities(List authorities) {
		this.authorities = authorities;
	}

	private static final long serialVersionUID = 1L;
	private RoleBo roleBo;
	private UserBo userBo;
	private List roleList;
	private List authorities;
	private Integer roleNo;
	private Role role;
}
